﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace 行政区域
{
    /// <summary>
    /// 类Xzqy。
    /// </summary>
    [Serializable]
    public partial class Xzqy
    {
        public Xzqy()
        {
            ID = Guid.NewGuid();
            ParentId = Guid.Empty;
            AddDate = DateTime.Now;
            IsDelete = false;
            UpdateDate = DateTime.Now;
            Intro = "";
            IsLast = false;
        }
        #region Model
        private Guid _id;
        private string _helpercode;
        private string _name;
        private string _intro;
        private bool _islast;
        private int _level;
        private string _nodepath;
        private int _orderby;
        private Guid _parentid;
        private DateTime? _adddate;
        private DateTime? _updatedate;
        public bool IsDelete { get; set; }
        /// <summary>
        /// 编号
        /// </summary>
        public Guid ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 区划代码
        /// </summary>
        public string HelperCode
        {
            set { _helpercode = value; }
            get { return _helpercode; }
        }
        /// <summary>
        /// 区划名称
        /// </summary>
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }
        /// <summary>
        /// 区划介绍
        /// </summary>
        public string Intro
        {
            set { _intro = value; }
            get { return _intro; }
        }
        /// <summary>
        /// 是否最后一级
        /// </summary>
        public bool IsLast
        {
            set { _islast = value; }
            get { return _islast; }
        }
        /// <summary>
        /// 树形深度
        /// </summary>
        public int Level
        {
            set { _level = value; }
            get { return _level; }
        }
        /// <summary>
        /// 树形路径
        /// </summary>
        public string NodePath
        {
            set { _nodepath = value; }
            get { return _nodepath; }
        }
        /// <summary>
        /// 排序
        /// </summary>
        public int OrderBy
        {
            set { _orderby = value; }
            get { return _orderby; }
        }
        /// <summary>
        /// 上一级
        /// </summary>
        public Guid ParentId
        {
            set { _parentid = value; }
            get { return _parentid; }
        }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime? AddDate
        {
            set { _adddate = value; }
            get { return _adddate; }
        }
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateDate
        {
            set { _updatedate = value; }
            get { return _updatedate; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Xzqy]");
            strSql.Append(" where ID=@ID ");

            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Xzqy] (");
            strSql.Append("ID,HelperCode,Name,Intro,IsLast,Level,NodePath,OrderBy,ParentId,AddDate,UpdateDate,IsDelete)");
            strSql.Append(" values (");
            strSql.Append("@ID,@HelperCode,@Name,@Intro,@IsLast,@Level,@NodePath,@OrderBy,@ParentId,@AddDate,@UpdateDate,@IsDelete)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@HelperCode", SqlDbType.NVarChar,-1),
					new SqlParameter("@Name", SqlDbType.NVarChar,-1),
					new SqlParameter("@Intro", SqlDbType.NVarChar,-1),
					new SqlParameter("@IsLast", SqlDbType.Bit,1),
					new SqlParameter("@Level", SqlDbType.Int,4),
					new SqlParameter("@NodePath", SqlDbType.NVarChar,-1),
					new SqlParameter("@OrderBy", SqlDbType.Int,4),
					new SqlParameter("@ParentId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@UpdateDate", SqlDbType.DateTime),
                    new SqlParameter("@IsDelete", SqlDbType.Bit)};
            parameters[0].Value = ID;
            parameters[1].Value = HelperCode;
            parameters[2].Value = Name;
            parameters[3].Value = Intro;
            parameters[4].Value = IsLast;
            parameters[5].Value = Level;
            parameters[6].Value = NodePath;
            parameters[7].Value = OrderBy;
            parameters[8].Value = ParentId;
            parameters[9].Value = AddDate;
            parameters[10].Value = UpdateDate;
            parameters[11].Value = IsDelete;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,HelperCode,Name,Intro,IsLast,Level,NodePath,OrderBy,ParentId,AddDate,UpdateDate ");
            strSql.Append(" FROM [Xzqy] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    this.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HelperCode"] != null)
                {
                    this.HelperCode = ds.Tables[0].Rows[0]["HelperCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null)
                {
                    this.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Intro"] != null)
                {
                    this.Intro = ds.Tables[0].Rows[0]["Intro"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsLast"] != null && ds.Tables[0].Rows[0]["IsLast"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsLast"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsLast"].ToString().ToLower() == "true"))
                    {
                        this.IsLast = true;
                    }
                    else
                    {
                        this.IsLast = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Level"] != null && ds.Tables[0].Rows[0]["Level"].ToString() != "")
                {
                    this.Level = int.Parse(ds.Tables[0].Rows[0]["Level"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NodePath"] != null)
                {
                    this.NodePath = ds.Tables[0].Rows[0]["NodePath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["OrderBy"] != null && ds.Tables[0].Rows[0]["OrderBy"].ToString() != "")
                {
                    this.OrderBy = int.Parse(ds.Tables[0].Rows[0]["OrderBy"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    this.ParentId = new Guid(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"] != null && ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    this.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdateDate"] != null && ds.Tables[0].Rows[0]["UpdateDate"].ToString() != "")
                {
                    this.UpdateDate = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
                }
            }
        }

        #endregion  Method
    }
}
